<?
class iCombo 
{
	var $query;				// query creata
	var $border;			// visuaizza bordo
	var $form;				// nome form che contiene la griglia
	var $adodb;				// classe adodb
	var $rs;				// connesione adodb
	var $combo_name;			// nome griglia
	var $combo_style;		// classe stile griglia
	var $combo_width;		// larghezza griglia
	var $combo_height;		// altezza griglia
	var $combo_onclick;		// onclick select
	var $combo_onchange;	// onchange select
	var $combo_first_selected;	// nome campo per selezionare il primo valore
	var $combo_ctrl_field;	// campo da controllare che se variato verra cancellato il
							// campo dichiarato in combo_reset_field
	var $combo_reset_field;

	var $row_num;			// numero righe da visualizzare
	var $row_style;			// stile righe
	var $row_onclick;		// azione nel caso della selezione della riga
	var $row_onclick_if;	// condizione per la scelta dell'azione sulla riga
	var $row_height;		// altezza della riga
	var $row_type;			// NORMAL viene mostrato solo un tipo di stile per le righe
							// ALTERNATE viene mostrato piu di un tipo di stile per le righe
	var $pos_row=0;			// contatore posizione righe
	var $pos_row_onclick=0;	// contatore 

	var $col_name;			// nome colonna
	var $col_style;			// stile colonna
	var $col_onclick;		// azione javascript in caso di selezione da pulsante o riferimento
	var $col_select;		// utilizzata per creare la select
	var $col_format;		// stabilisce il formato di visualizzazione
	var $col_image;			// un immagine da inserire nella colonna
	var	$col_img_width;		// larghezza immagine
	var $col_img_height;	// altezza immagine
	var $col_visible;		// 0 o 1 se la colonna e visibile assiociato con la select per 
	var $col_index;
	var $col_width;			// larghezza colonna in percentuale
	var $col_height;		// altezza colonna in percentuale
	var $col_type;			// BUTTON disegno pulsante x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// HREF   link di riferimento x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
	var $col_style_link;	// stile button
	var $col_pos;			// posizione colonna
	var $num_col=0;			// posizione colonna visibili
	var $pos_col=0;			// contatore posizione colonna

	var $font_size;			// dimensione font
	var $from;
	var $where;
	var $path_images;

	var $curr_page=1;		// pagine attualmente visualizzata
	var $pages=0;			// pagine totali
	var $page_up_style;		// stile pulsante pagina avanti
	var $page_down_style;	// stile pulsante pagina avanti
	var $orderby="";		// posizione campo dell'ordinamento

	var $err;
	var $a;
	var $kname;
	var $name;
	var $tkey;
	var $tval;
	var $tstr;
	var $tr_cnt;
	var $tr_tmp;
	var $option_selected;
	var $tpost;

	var $request = array();
	var $session = array();
	var $outHtml = '';

	function iCombo() 
	{
		foreach($this->request as $this->tkey => $this->tval) {
			if ($this->tkey=="curr_page") $this->curr_page=$this->tval;
			if ($this->tkey=="pages") $this->pages=$this->tval;
			if ($this->tkey=="orderby") $this->orderby=$this->tval;
		}

		if ($this->curr_page < 1) $this->curr_page = 1;
		if ($this->curr_page > $this->pages && $this->pages > 0) $this->curr_page = $this->pages;

		$this->row_style=array();
		$this->col_name=array();
		$this->col_style=array();
		$this->col_onclick=array();
		$this->col_orderby=array();
		$this->col_select=array();
		$this->col_format=array();
		$this->col_image=array();
		$this->col_visible=array();
		$this->col_width=array();
		$this->col_height=array();
		$this->from=array();
		$this->where=array();

		$this->row_style=array();
		$this->row_onclick=array();
		$this->row_height=array();

	}
	
	function rows_select($onclick="",$if="") {
		$this->row_onclick[$this->pos_row_onclick]=$onclick;
		$this->row_onclick_if[$this->pos_row_onclick]=$if;
		$this->pos_row_onclick++;
	}
	
	function rows($style="",$height=0) {
		$this->row_style[$this->pos_row]=$style;
		$this->row_height[$this->pos_row]=$height;
		$this->pos_row++;
	}

	function select_add(&$select) {
		$name=$select["name"];
		$this->col_select[$name]=$select["field"];		// campo database
		$this->col_name[$name]=$select["name"];			// nome griglia
		$this->col_pos[$name]=$this->pos_col;					// posizione griglia
		$this->col_style[$name]=$select["style"];			// style colonna
		$this->col_onclick[$name]=$select["onclick"];		// funzione onclick
		$this->col_format[$name]=$select["format"];		// formato di visualizzazione
		$this->col_image[$name]=$select["image"];			// immagine da vis. in colonna
		$this->col_img_width[$name]=$select["img_w"];		// larghezza imagine
		$this->col_img_height[$name]=$select["img_h"];	// altezza immagine
		$this->col_visible[$name]=$select["visible"];		// colonna visibile
		$this->col_width[$name]=$select["width"];			// larghezza colonna
		$this->col_height[$name]=$select["height"];		// altezza colonna
		$this->col_type[$name]=$select["type"];			// tipo NONE, BUTTON, HREF
		$this->col_style_link[$name]=$select["stybtn"];	// stile button
		$this->pos_col++;
		if ($select["visible"]==1) $this->num_col++;
	}

	function orderby($field,$name) {
		$this->col_orderby[$name]=$field;
	}

	function view_query() {
		print $this->query."<BR>";
	}

	function make_query() {
		$this->query="SELECT ";
		$this->a=0;
		foreach($this->col_select as $this->name => $this->field) {
			if ($this->a==1) {
				$this->query .= ","; 
			} else {
				$this->a=1;
			}
			$this->query .= $this->field." AS ".$this->name." ";			
		}
		$this->query .= "FROM ".$this->from." ";
		if ($this->where!="") $this->query .= "WHERE ".$this->where." ";

		if ($this->orderby!="") {
			settype($this->orderby,"integer");
			if ($this->orderby < 0) {
				$this->query .= "ORDER BY ".($this->orderby * -1)." DESC";
			} else {
				$this->query .= "ORDER BY ".$this->orderby." ASC";
			}
		}
	}
/*
------------------------------------------------------------------------------------------------------------
	DISEGNA GRIGLIA
------------------------------------------------------------------------------------------------------------
*/

	function show() 
	{
		if ($this->combo_ctrl_field!="") 
		{
			if (isset($this->request[$this->combo_ctrl_field]) && isset($this->session[$this->combo_ctrl_field])) 
			{
				if ($this->request[$this->combo_ctrl_field] != $this->session[$this->combo_ctrl_field]) 
				{
					unset($this->request[$this->combo_reset_field]);
				}
			}
		}

		$this->make_query();

		$this->rs = $this->adodb->Execute($this->query);
		if (!$this->rs) 
		{
	  		$this->err=$this->adodb->ErrorMsg();
			return "ERR";
		}

		$this->outHtml.=  "<SELECT name=\"".$this->combo_name."\" id=\"".$this->combo_name."\" ";
		if ($this->combo_style!="") $this->outHtml.=  "class=\"".$this->combo_style."\" ";
		if ($this->row_num==0) 
		{
			$this->outHtml.=  "size=\"1\" ";
		} 
		else 
		{
			$this->outHtml.=  "size=\"".$this->row_num."\" ";
		}

		if ($this->combo_onclick!="") $this->outHtml.=  "onclick=\"".$this->combo_onclick."\" ";
		if ($this->combo_onchange!="") $this->outHtml.=  "onchange=\"".$this->combo_onchange."\" ";

		if ($this->combo_height!=0) $this->outHtml.=  "height=\"".$this->combo_height."px\" ";

		if ($this->combo_width!=0) $this->outHtml.=  "width=\"".$this->combo_width."px\" ";

		$this->option_selected="*";
		if (isset($this->request[$this->combo_first_selected])) 
		{
			if ($this->request[$this->combo_first_selected]!="") $this->option_selected=$this->request[$this->combo_first_selected];
		}

		$this->outHtml.=  ">\n";

		if ($this->option_selected=="*") $this->outHtml.=  "<OPTION value=\"\" selected></OPTION>\n";

		$this->tr_cnt=0;
		$this->tr_tmp=count($this->row_style) - 1;
		while (!$this->rs->EOF) 
		{
			$this->outHtml.=  "<OPTION ";
			if ($this->tr_tmp>0) 
			{
				if ($this->row_type=="ALTERNATE") 
				{
					if ($this->tr_cnt > $this->tr_tmp) $this->tr_cnt=0;
					$this->outHtml.=  "class=\"".$this->row_style[$this->tr_cnt]."\" ";
					$this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
					$this->tr_cnt++;
				} 
				else 
				{
					if ($this->row_style[0]!="") $this->outHtml.=  "class=\"".$this->row_style[0]."\" ";
					if ($this->row_height[0]!=0) $this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
				}
				
				if ($this->pos_row_onclick>0) 
				{
					foreach($this->row_onclick as $this->tkey => $this->tval) 
					{
						$this->tstr=$this->tval;
						if ($this->row_onclick_if[$this->tkey]=="") 
						{
							foreach($this->col_name as $this->tkey => $this->tval) 
							{
								if (eregi("%".$this->tval."%",$this->tstr)) 
								{
									$this->tstr=str_replace("%".$this->tval."%",$this->rs->fields[$this->col_pos[$this->tval]],$this->tstr);
								}
							}
						}
						$this->outHtml.=  "onclick=\"".$this->tstr."\" ";
					}
				}
			}
			$this->outHtml.=  "value=\"".$this->rs->fields[0]."\" ";
			if ($this->option_selected==$this->rs->fields[0]) $this->outHtml.=  "selected";
			$this->outHtml.=  ">";
/*
------------------------------------------------------------------------------------------------------------
	Disegna Righe
------------------------------------------------------------------------------------------------------------
*/
			$this->outHtml.=  $this->rs->fields[1]."</OPTION>\n";
			$this->rs->MoveNext();
		}
		$this->outHtml.=  "</SELECT>\n";
	}

	/* Calcolo risoluzione in px dalla percentuale data e la risoluzione prestabilita*/
	function ris_x($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->combo_width * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function ris_y($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->combo_height * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	/* Calcolo posizione in px data dalla somma percentuale complessiva degli oggetti 
		che precedono l'attuale
	*/
	function pos_x($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->combo_width * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function pos_y($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->combo_height * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function button($name) {
		$this->outHtml.=  "<INPUT type=\"button\" ";
		if ($this->col_style_link[$name]!="") $this->outHtml.=  "class=\"".$this->col_style_link[$name]."\" ";
		if ($this->col_onclick[$name]!="")  {
			$this->tstr=$this->col_onclick[$name];
			foreach($this->col_name as $this->tkey => $this->tval) {
				if (eregi("%".$this->tval."%",$this->tstr)) {
					$this->tstr=str_replace("%".$this->tval."%",$this->rs->fields[$this->col_pos[$this->tval]],$this->tstr);
				}
			}
			$this->outHtml.=  "onclick=\"".$this->tstr."\" ";
		}
		$this->outHtml.=  "value=\"".$this->rs->fields[$this->col_pos[$name]]."\"";
		$this->outHtml.=  ">\n";
	}
	
	function hbutton($name,$pos) {
		$this->outHtml.=  "<INPUT type=\"button\" ";
		$this->outHtml.=  "onclick=\"order(".$pos.")\" ";
		$this->outHtml.=  "value=\"".$name."\"";
		$this->outHtml.=  ">\n";
	}

	function href($name) {
		$this->outHtml.=  "<A href=\"#\" ";
		if ($this->col_style_link[$name]!="") $this->outHtml.=  "class=\"".$this->col_style_link[$name]."\" ";
		if ($this->col_onclick[$name]!="")  {
			$this->tstr=$this->col_onclick[$name];
			foreach($this->col_name as $this->tkey => $this->tval) {
				if (eregi("%".$this->tval."%",$this->tstr)) {
					$this->tstr=str_replace("%".$this->tval."%",$this->rs->fields[$this->col_pos[$this->tval]],$this->tstr);
				}
			}
			$this->outHtml.=  "onclick=\"".$this->tstr."\" ";
		}
		$this->outHtml.=  ">\n";
		if ($this->col_image[$name]=="") {
			$this->outHtml.=  $this->rs->fields[$this->col_pos[$name]];
		} else {
			$this->outHtml.=  "<IMG src=\"".$this->path_images."/".$this->col_image[$name]."\" width=\"".$this->ris_x($this->col_img_width[$name])."\" height=\"".$this->ris_y($this->col_img_height[$name])."\">\n";
		}
		$this->outHtml.=  "</A>\n";
	}

	function hhref($name,$pos) {
		$this->outHtml.=  "<A ";
		$this->outHtml.=  "onclick=\"order('".$pos."')\">\n";
		$this->outHtml.=  $name."\n";
		$this->outHtml.=  "</A>\n";
	}
}
?>
